<!--
 * @Author: spycym
 * @Date: 2022-11-09 15:25:16
 * @LastEditTime: 2022-11-16 23:43:46
 * @LastEditors: spycym
 * @Description: 
 * 不要随便动我的代码哟!
-->
<script lang="ts" setup>
import { ref, nextTick } from "vue";
import { useTodoStore } from "../store";
const todoStore = useTodoStore();
const todoProxy = defineProps(["todoObj"]);
const todoObj = todoProxy.todoObj;
function changeDone() {
  todoStore.editDone(todoObj.id);
}
// 编辑输入框
const keyWord = ref(todoObj.name);
// 编辑
let inputValue = ref();
function editHandler() {
  todoStore.editChange(todoObj.id);
  nextTick(() => {
    inputValue.value.focus();
  });
}
function EditTodoName() {
  if (!keyWord.value.trim()) return alert("请输入有效字符!");
  todoStore.editName(keyWord.value.trim(), todoObj.id);
}
// 删除
function deleteTodo() {
  if (confirm("你确定要删除该事件吗?")) {
    todoStore.deleteTodoObj(todoObj);
  }
}
</script>
<template>
  <li>
    <label>
      <input type="checkbox" :checked="todoObj.done" @change="changeDone" />
      <span v-show="!todoObj.isEdit">{{ todoObj.name }}</span>
      <input
        ref="inputValue"
        type="text"
        v-model="keyWord"
        v-show="todoObj.isEdit"
        @blur="EditTodoName"
      />
    </label>
    <button class="btn btn-danger" @click="deleteTodo">删除</button>
    <button class="btn btn-edit" @click="editHandler">编辑</button>
  </li>
</template>
<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
  margin-right: 10px;
}
li:hover button {
  display: block;
}
li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
</style>